package com.example.wechatappbackend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wechatappbackend.common.CommonResponse;
import com.example.wechatappbackend.entity.UserWechat;
import com.example.wechatappbackend.persistence.UserMapper;
import com.example.wechatappbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public boolean addNewUser(String openId, String nickName, String avatar) {
        QueryWrapper<UserWechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userID", openId);
        UserWechat userWechat = userMapper.selectOne(queryWrapper);
        System.out.println("openIdADD"+openId);
        if(userWechat == null){
            userWechat = new UserWechat();
            userWechat.setUserID(openId);
            userWechat.setWechatName(nickName);
            userWechat.setProfile(avatar);
            userMapper.insert(userWechat);
            return true;
        }
        else {
            return false;
        }


    }

    @Override
    public int checkIsExist(String openId) {
        QueryWrapper<UserWechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userID", openId);
        UserWechat userWechat = userMapper.selectOne(queryWrapper);
        if(userWechat == null){
            return 1;
        }
        else
            return 0;
    }
}
